import {
  auth
} from '../../utils/util'
import {
  categories,
  shops
} from '../../utils/api'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    city: '',
    categories: '',
    categoriesKey: '',
    longitude: '',
    latitude: '',
    shops: [],
    show: false,
    page: 1,
    keyWords: '',
    isOver: false,
    loading: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let systemInfo = wx.getStorageSync('systemInfo')
    this.setData({
      categoriesKey: 226,
      imageWidth: systemInfo.windowWidth * 0.3,
      imageHeight: systemInfo.windowWidth * 0.24
    })
    auth('scope.userLocation', '请允许使用您的位置').then(res => {
      if (res) {
        this.getLocation().then(res=>{
          if(res){
            this.doRequest()
          }
        })
      }
    })
  },
  /**
   * 获取位置
   */
  getLocation() {
    return new Promise((resolve, reject) => {
      wx.getLocation({
        success: res => {
          this.setData({
            latitude: res.latitude,
            longitude: res.longitude,
          })
          resolve(true)
        },
        fail: () => {
          resolve(false)
        }
      })
    })
  },
  /**
   * doRequest
   */
  doRequest() {
    wx.request({
      url: 'https://mp.tiyeji.com/MtfxAction.do',
      data: {
        dispatch: 'getCity',
        key: 'MtfxAction.getCity',
        location: this.data.longitude + ',' + this.data.latitude,
        isJson: 1
      },
      method: 'GET',
      success: res => {
        this.setData({
          city: res.data.city
        })
        this.categories()
      }
    })
  },
  // 查询类目
  categories() {
    categories({
      cityId: this.data.city.cityCode
    }).then(res => {
      this.setData({
        categories: res.data
      })
      this.shopInfos()
    })
  },
  // 选择类目
  onChooseCategory(e) {
    this.setData({
      categoriesKey: e.currentTarget.dataset.index,
      shops: [],
      keyWords: '',
      page: 1,
      show: true,
      isOver: false
    })
    this.shopInfos()
  },
  // 上拉触底函数
  onReachBottom: function () {
    if (!this.data.isOver) {
      this.setData({
        show: true
      })
      setTimeout(() => {
        this.shopInfos()
      }, 500);
    }
  },
  // 查询商铺信息
  shopInfos() {
    this.setData({
      show: true
    })
    shops({
      cat0Id: this.data.categoriesKey,
      lng: this.data.longitude,
      lat: this.data.latitude,
      cityId: this.data.city.cityCode,
      page: this.data.page++,
      size: 100,
      keyWords: this.data.keyWords,
    }).then(res => {
      if (res.data.records && res.data.records.length != 0) {
        this.setData({
          shops: this.data.shops.concat(res.data.records)
        })
      } else {
        this.setData({
          isOver: true,
        })
      }
      this.setData({
        show: false,
        loading: false
      })
    })
  },
  // 跳转到团单详情页面
  tuandanDetail(e) {
    let param = {
      cat0Id: this.data.categoriesKey,
      lng: this.data.longitude,
      lat: this.data.latitude,
      cityId: this.data.city.cityCode,
      shopIds: this.data.shops[e.currentTarget.dataset.index].shopInfo.shopId
    }
    wx.navigateTo({
      url: '../tuandanDetail/tuandanDeatail?param=' + JSON.stringify(param),
    })
  },
  // 按关键字搜索
  search() {
    this.setData({
      shops: [],
      page: 1
    })
    this.shopInfos()
  }
})